/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ARPPanel.java
 *
 * Created on 2010-01-24, 02:02:32
 */

package efe.gui;

import efe.bean.EfeByteValue;
import efe.bean.EfeMACValue;
import efe.bean.EfeNumberValue;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Vector;
import jpcap.packet.ARPPacket;

/**
 * Panel z nagłówkiem protokołu ARP.
 * @author root
 */
public class ARPPanel extends javax.swing.JPanel implements Serializable {

    /** Creates new form ARPPanel */
    public ARPPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        hardwareType = new efe.bean.EfeNumberValue();
        protocolType = new efe.bean.EfeNumberValue();
        hardwareLen = new efe.bean.EfeNumberValue();
        protocolLen = new efe.bean.EfeNumberValue();
        operation = new efe.bean.EfeNumberValue();
        sha = new efe.bean.EfeMACValue();
        shaNext = new efe.bean.EfeMACValue();
        spa = new efe.bean.EfeByteValue();
        spaNext = new efe.bean.EfeByteValue();
        tha = new efe.bean.EfeMACValue();
        thaNext = new efe.bean.EfeMACValue();
        tpa = new efe.bean.EfeByteValue();
        jPanel1 = new javax.swing.JPanel();
        hardwareTypeField = new efe.bean.EfeEditorField();
        protocolTypeField = new efe.bean.EfeEditorField();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        hardLenField = new efe.bean.EfeEditorField();
        protLenField = new efe.bean.EfeEditorField();
        operationField = new efe.bean.EfeEditorField();
        shaField = new efe.bean.EfeEditorField();
        jPanel4 = new javax.swing.JPanel();
        shaNextField = new efe.bean.EfeEditorField();
        spaField = new efe.bean.EfeEditorField();
        jPanel5 = new javax.swing.JPanel();
        spaNextField = new efe.bean.EfeEditorField();
        thaField = new efe.bean.EfeEditorField();
        thaNextField = new efe.bean.EfeEditorField();
        tpaField = new efe.bean.EfeEditorField();

        hardwareType.setLimit(65535);
        hardwareType.validate(String.valueOf(ARPPacket.HARDTYPE_ETHER));

        protocolType.setLimit(65535);
        protocolType.validate(String.valueOf(ARPPacket.PROTOTYPE_IP));

        hardwareLen.setLimit(255);
        hardwareLen.validate("6");

        protocolLen.setLimit(255);
        protocolLen.validate("4");

        operation.setLimit(65535);
        operation.validate(String.valueOf(ARPPacket.ARP_REQUEST));

        sha.setLength(4);
        sha.validate("FF:FF:FF:FF");

        shaNext.setLength(2);
        shaNext.validate("FF:FF");

        spa.setLength(2);
        spa.validate("0x7F 0x00");

        spaNext.setLength(2);
        spaNext.validate("0x00 0x01");

        tha.setLength(2);
        tha.validate("FF:FF");

        thaNext.setLength(4);
        thaNext.validate("FF:FF:FF:FF");

        tpa.setLength(4);
        tpa.validate("0x7F 0x00 0x00 0x01");

        setLayout(new java.awt.GridLayout(7, 1));

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        hardwareTypeField.setText("Hardware type");
        hardwareTypeField.setLabelText("Hardware type");
        hardwareTypeField.setValue(hardwareType);
        jPanel1.add(hardwareTypeField);

        protocolTypeField.setText("Protocol type");
        protocolTypeField.setLabelText("Protocol type");
        protocolTypeField.setValue(protocolType);
        jPanel1.add(protocolTypeField);

        add(jPanel1);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jPanel3.setLayout(new java.awt.GridLayout(1, 0));

        hardLenField.setText("Hardware address length");
        hardLenField.setLabelText("Hardware address length");
        hardLenField.setValue(hardwareLen);
        jPanel3.add(hardLenField);

        protLenField.setText("Protocol address length");
        protLenField.setLabelText("Protocol address length");
        protLenField.setValue(protocolLen);
        jPanel3.add(protLenField);

        jPanel2.add(jPanel3);

        operationField.setText("Operation");
        operationField.setLabelText("Operation");
        operationField.setValue(operation);
        jPanel2.add(operationField);

        add(jPanel2);

        shaField.setText("Sender hardware adderss ");
        shaField.setLabelText("Sender hardware adderss ");
        shaField.setValue(sha);
        add(shaField);

        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        shaNextField.setText("(next two octets)");
        shaNextField.setLabelText("(next two octets)");
        shaNextField.setValue(shaNext);
        jPanel4.add(shaNextField);

        spaField.setText("Sender protocol address ");
        spaField.setInheritsPopupMenu(true);
        spaField.setLabelText("Sender protocol address ");
        spaField.setValue(spa);
        jPanel4.add(spaField);

        add(jPanel4);

        jPanel5.setLayout(new java.awt.GridLayout(1, 0));

        spaNextField.setText("(next two octets)");
        spaNextField.setLabelText("(next two octets)");
        spaNextField.setValue(spaNext);
        jPanel5.add(spaNextField);

        thaField.setText("Target hardware address");
        thaField.setLabelText("Target hardware address");
        thaField.setValue(tha);
        jPanel5.add(thaField);

        add(jPanel5);

        thaNextField.setText("(next four octets)");
        thaNextField.setLabelText("(next four octets)");
        thaNextField.setValue(thaNext);
        add(thaNextField);

        tpaField.setText("Target protocol address");
        tpaField.setLabelText("Target protocol address");
        tpaField.setValue(tpa);
        add(tpaField);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Tworzy pakiet ARP z pól nagłówka, gotowy do tego żeby osadzić go
     * w warstwie łącza danych.
     * @return pakiet
     */
    public ARPPacket getARPPacket() {
        ARPPacket p = new ARPPacket();
        p.hardtype = (short) hardwareType.getValue();
        p.prototype = (short) protocolType.getValue();
        p.hlen = (short) hardwareLen.getValue();
        p.plen = (short) protocolLen.getValue();
        p.operation = (short) operation.getValue();
        p.sender_hardaddr = concatenateByteArrays((byte[]) sha.getValue(),
                (byte[]) shaNext.getValue(), p.hlen);
        p.sender_protoaddr = concatenateByteArrays((byte[]) spa.getValue(),
                (byte[]) spaNext.getValue(), p.plen);
        p.target_hardaddr = concatenateByteArrays((byte[]) tha.getValue(),
                (byte[]) thaNext.getValue(), p.hlen);
        p.target_protoaddr = (byte[]) tpa.getValue();
        return p;
    }

    private byte[] concatenateByteArrays(byte[] a1, byte[] a2, int len) {
        byte[] b = new byte[len];
        int a1Len = a1.length;
        if (a1Len > len) { a1Len = len; }
        for (int i = 0; i < a1Len; ++i) {
            b[i] = a1[i];
        }
        for (int i =0; i < (len - a1Len); ++i) {
            b[a1Len + i] = a2[i];
        }
        return b;
    }

    /**
     * Zwraca wektor serializowalnych wartości, które należy zapisać do pliku.
     * @return kolekcja wartości nagłówka
     */
    public Vector<Object> getValuesVector() {
        Vector<Object> v = new Vector<Object>();
        v.add(hardwareLen);
        v.add(hardwareType);
        v.add(operation);
        v.add(protocolLen);
        v.add(protocolType);
        v.add(sha);
        v.add(shaNext);        
        v.add(spa);
        v.add(spaNext);
        v.add(tha);
        v.add(thaNext);
        v.add(tpa);
        return v;
    }

    /**
     * Zapisuje dane z wektora do pól nagłówka.
     * @param v wektor wartości z elementami nagłówka w kolejności takiej samej
     * jak ta zwrócona przez metodę getValuesVector()
     */
    public void setValuesFromVector(Vector<Object> v) {
        Iterator<Object> i = v.iterator();
        hardwareLen = (EfeNumberValue) i.next();
	hardLenField.setValue(hardwareLen);
        hardwareType = (EfeNumberValue) i.next();
	hardwareTypeField.setValue(hardwareType);
        operation = (EfeNumberValue) i.next();
	operationField.setValue(operation);
        protocolLen = (EfeNumberValue) i.next();
	protLenField.setValue(protocolLen);
        protocolType = (EfeNumberValue) i.next();
	protocolTypeField.setValue(protocolType);
        sha = (EfeMACValue) i.next();
	shaField.setValue(sha);
        shaNext = (EfeMACValue) i.next();
	shaNextField.setValue(shaNext);
        spa = (EfeByteValue) i.next();
	spaField.setValue(spa);
        spaNext = (EfeByteValue) i.next();
	spaNextField.setValue(spaNext);
        tha = (EfeMACValue) i.next();
	thaField.setValue(tha);
        thaNext = (EfeMACValue) i.next();
	thaNextField.setValue(thaNext);
        tpa = (EfeByteValue) i.next();
	tpaField.setValue(tpa);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private efe.bean.EfeEditorField hardLenField;
    private efe.bean.EfeNumberValue hardwareLen;
    private efe.bean.EfeNumberValue hardwareType;
    private efe.bean.EfeEditorField hardwareTypeField;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private efe.bean.EfeNumberValue operation;
    private efe.bean.EfeEditorField operationField;
    private efe.bean.EfeEditorField protLenField;
    private efe.bean.EfeNumberValue protocolLen;
    private efe.bean.EfeNumberValue protocolType;
    private efe.bean.EfeEditorField protocolTypeField;
    private efe.bean.EfeMACValue sha;
    private efe.bean.EfeEditorField shaField;
    private efe.bean.EfeMACValue shaNext;
    private efe.bean.EfeEditorField shaNextField;
    private efe.bean.EfeByteValue spa;
    private efe.bean.EfeEditorField spaField;
    private efe.bean.EfeByteValue spaNext;
    private efe.bean.EfeEditorField spaNextField;
    private efe.bean.EfeMACValue tha;
    private efe.bean.EfeEditorField thaField;
    private efe.bean.EfeMACValue thaNext;
    private efe.bean.EfeEditorField thaNextField;
    private efe.bean.EfeByteValue tpa;
    private efe.bean.EfeEditorField tpaField;
    // End of variables declaration//GEN-END:variables

}
